﻿// Copyright 2008 Figbrain
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Text;
using Castle.Core.Resource;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;

namespace Nextworks.Core.Services
{
    public sealed class ServiceContainer
    {
        private static ServiceContainer _server;
        private readonly IWindsorContainer _container;

        public static ServiceContainer Instance
        {
            get
            {
                if (_server == null)
                {
                    _server = new ServiceContainer();

                }

                return (_server);
            }
        }
        
        private ServiceContainer()
        {
            _container = new WindsorContainer(new XmlInterpreter(new AssemblyResource("Nextworks.Core/Nextworks.Core.Properties/castle.xml")));
        }


        public bool IsAvailable(string key)
        {
            return (_container.Kernel.HasComponent(key));
        }

        public bool IsAvailable(Type t)
        {
            return (_container.Kernel.HasComponent(t));
        }

        public T Resolve<T>(string key)
        {
            return (_container.Resolve<T>(key));
        }

        public T Resolve<T>()
        {
            return (_container.Resolve<T>());
        }

        public void Release(object source)
        {
            _container.Release(source);
        }

        public object this[string key]
        {
            get
            {
                return (this._container[key]);
            }
        }
    }
}
